package util.SocketUtil;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketUtil {

    private static final  String IP="0.0.0.0";

    private static final int PORT=8888;

    public static <T,V> V sendMessage(Message<T> msg){
        try {
            Socket client=new Socket(IP,PORT);

            OutputStream os=client.getOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(os);
            oos.writeObject(msg);
            oos.flush();
            client.shutdownOutput();

            InputStream is=client.getInputStream();
            ObjectInputStream ois=new ObjectInputStream(is);
            V result= (V) ois.readObject();
            client.shutdownInput();

            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
